//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.Expressions
{
    class PercentExpression: UnaryExpressionBase
    {
        public PercentExpression(Expression inner) : base(inner)
        {
        }

        public override string GetText(TextContext context)
        {
            return Inner.GetText(context) + "%";
        }
        internal override Expression Transposed(Cell target)
        {
            return Inner.Transposed(target);
        }

        protected override object GetObjectImp(EvaluationContext context)
        {
            object val = Inner.GetValue(context) ?? 0.0d;
            if (val is Error)
                return val;
            if (val is DateTime)
                //val = DateTimeConverter.DateTimeToDouble((DateTime)val context.Worksheet.Workbook.d);
                val = context.Worksheet.Workbook.DateTimeToDouble((DateTime) val);
            if (val is bool)
            {
                val = ((bool)val) ? 1 : 0;
            }
            if (val is double)
                return ((double) val)/100;
            return Errors.ValueError;
        }

        public override Expression Offset(CellRelativeAddress a)
        {
            return new PercentExpression(Inner.Offset(a));
        }
    }
}